<template>
	<el-drawer v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :size="1000">
  <el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" :label-width="100">
  <el-form-item label="评论ID" prop="pinglunId">
      <el-input v-model="dataForm.pinglunId" placeholder="评论ID"></el-input>
  </el-form-item>
  <el-form-item label="论坛ID" prop="forumId">
      <el-input v-model="dataForm.forumId" placeholder="论坛ID"></el-input>
  </el-form-item>
  <el-form-item label="帖子ID" prop="tieId">
      <el-input v-model="dataForm.tieId" placeholder="帖子ID"></el-input>
  </el-form-item>
  <el-form-item label="举报类型" prop="reporttype">
      <el-input v-model="dataForm.reporttype" placeholder="举报类型"></el-input>
  </el-form-item>
  <el-form-item label="举报理由" prop="reportreason">
      <el-input v-model="dataForm.reportreason" placeholder="举报理由"></el-input>
  </el-form-item>
  <el-form-item label="联系人电话" prop="contactsphone">
      <el-input v-model="dataForm.contactsphone" placeholder="联系人电话"></el-input>
  </el-form-item>
  </el-form>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="submitHandle()">确定</el-button>
		</template>
	</el-drawer>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import { useReportApi, useReportSubmitApi } from '@/api/kyy/report'

const emit = defineEmits(['refreshDataList'])

const visible = defineModel<boolean>('visible')
const dataFormRef = ref()

const dataForm = reactive({
	id: '',
	pinglunId: '',
	forumId: '',
	tieId: '',
	reporttype: '',
	reportreason: '',
	contactsphone: ''})

const init = (id?: number) => {
	if (id) {
		getReport(id)
	}
}

const getReport = (id: number) => {
	useReportApi(id).then(res => {
		Object.assign(dataForm, res.data)
	})
}

const dataRules = ref({
	pinglunId: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	forumId: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	tieId: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	reporttype: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	reportreason: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	contactsphone: [{ required: true, message: '必填项不能为空', trigger: 'blur' }]})

// 表单提交
const submitHandle = () => {
	dataFormRef.value.validate(async (valid: boolean) => {
		if (!valid) {
			return false
		}


		useReportSubmitApi(dataForm).then(() => {
			ElMessage.success({
				message: '操作成功',
				duration: 500,
				onClose: () => {
					visible.value = false
					emit('refreshDataList')
				}
			})
		})
	})
}

defineExpose({
	init
})
</script>
